﻿//==============================================================================
// File: DialogController.cs
// Created: 2010-05-10
// Author: Piotr Włodek
//==============================================================================
// This file is a part of MAMMOTH project.
// Copyright (C) 2010 AGH University of Science and Technology, Krakow.
// https://caribou.iisg.agh.edu.pl/trac/mammoth
//
// Mammoth is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// Mammoth is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public License
// along with Mammoth. If not, see http://www.gnu.org/licenses/.
//==============================================================================

using System;
using System.Text;
using WpfRcp.Core.Controllers;
using Microsoft.Win32;

namespace WpfRcp.Controllers
{
    internal class DialogController : IDialogController
    {
        public DialogResult OpenFileDialog(string title, string[] filterNames, string[] filters, bool allowMultiSelect)
        {
            var filter = PreprocessFilters(filterNames, filters);
            var openFileDialog = new OpenFileDialog { Title = title, Filter = filter, Multiselect = allowMultiSelect };
            bool? result = openFileDialog.ShowDialog();

            if (result == true)
                return new DialogResult(true, openFileDialog.FileName, openFileDialog.FileNames);

            return new DialogResult(false, null, null);
        }

        public DialogResult SaveFileDialog(string title, string[] filterNames, string[] filters)
        {
            return SaveFileDialog(title, filterNames, filters, null);
        }

        public DialogResult SaveFileDialog(string title, string[] filterNames, string[] filters, string defaultExtension)
        {
            var filter = PreprocessFilters(filterNames, filters);
            var saveFileDialog = new SaveFileDialog { Title = title, Filter = filter, DefaultExt = defaultExtension };
            bool? result = saveFileDialog.ShowDialog();

            if (result == true)
                return new DialogResult(true, saveFileDialog.FileName, null);

            return new DialogResult(false, null, null);
        }

        public DialogResult OpenDirectoryDialog(string title)
        {
            var folderBrowserDialog = new System.Windows.Forms.FolderBrowserDialog { ShowNewFolderButton = true, Description = title };
            var result = folderBrowserDialog.ShowDialog();

            if (result == System.Windows.Forms.DialogResult.OK)
                return new DialogResult(true, folderBrowserDialog.SelectedPath, null);

            return new DialogResult(false, null, null);
        }

        private static string PreprocessFilters(string[] filterNames, string[] filters)
        {
            var sb = new StringBuilder();
            for (int i = 0; i < filterNames.Length; i++)
            {
                sb.Append(filterNames[i]);
                sb.Append(" (");
                sb.Append(filters[i]);
                sb.Append(")|");
                sb.Append(filters[i]);

                if (i < filterNames.Length - 1)
                    sb.Append("|");
            }

            return sb.ToString();
        }
    }
}